package com.ssl.basics.list;

import java.util.*;
import java.util.concurrent.CountDownLatch;

/**
 * 描述：HashSet 不是线程安全的，当多线程进行写的时候，会出现重复的数据插入
 *
 * @author shi_senlin
 * @create 2021/07/10 7:55
 */
public class SetUnSafe {
    static Set<Integer> set = new HashSet<>();

    public static void main(String[] args) throws InterruptedException {

        CountDownLatch countDownLatch = new CountDownLatch(5);

        for (int i = 0; i < 5; i++) {
            Thread t = new Thread(() -> {
                for (int j=0;j<100;j++) {
                    set.add(j);
                }
                countDownLatch.countDown();// countDown 需写在线程的 run 方法内
            }, "AA");
            t.start();
        }
        countDownLatch.await();
        Arrays.sort(set.toArray());
        System.out.println(set);
    }
}
